/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RMI;

import java.sql.ResultSet;

/**
 *
 * @author TNT
 */
public class Search {
    Connect connect ;
    String chuoi[];
    ResultSet rs;
    public Search(String chuoi[]){
        this.chuoi = chuoi;
        connect = new Connect(chuoi);
    }
    
    public String[] searchProduct(String name, String provider, String cost){
        java.util.List<String> Product= new java.util.ArrayList<String>();
        String sql ="select id from product where";
        
        if(name.equals("")==false) sql =sql+"name like '%"+name+"%' and ";
        if(provider.equals("")==false) sql = sql+"provider ='"+provider+"' and ";
        if(cost.equals("")==false) sql =sql+"cost"+cost+"and ";
        sql = sql+"id IN (select id from product)";
        
       try{
           rs = connect.st.executeQuery(sql);        
           while(rs.next()){
                String Info = rs.getString(1)+"###"+rs.getString(2)+"###"+rs.getString(4)+"###"+rs.getString(5);
                Product.add(Info);
           }
       }
       catch(Exception ex){}
       
       int count = Product.size();
       String Prd[] = new String[count];
       System.out.println(Product.size());
       for(int i = 0; i<count; i++){
           Prd[i] = Product.remove(0);
       }
       return Prd;
    }
    public String[] searchProduct(String keyword){
        java.util.List<String> Product= new java.util.ArrayList<String>();
        String sql = "select * from Product where Provider = '"+keyword+"'";
        try{
            rs = connect.st.executeQuery(sql);
            while(rs.next()){
                String name = rs.getString("name");
                String provider = rs.getString("provider");
                String id = rs.getString("id");
                //if(name.contains(keyword)||provider.contains(keyword))
                    Product.add(id); 
            }
            }   
            catch(Exception ex){}
             int count = Product.size();
               String Prd[] = new String[count];
            for(int i =0;i<count;i++)
                Prd[i] = Product.remove(0);
        return Prd;
    }
    
}
